#ifndef THINK_THINK_MODEL_RELATION_HASONE_H
#define THINK_THINK_MODEL_RELATION_HASONE_H

static char* think_model_relation_hasone =
	"\n"
	"// +----------------------------------------------------------------------\n"
	"// | ThinkPHP [ WE CAN DO IT JUST THINK ]\n"
	"// +----------------------------------------------------------------------\n"
	"// | Copyright (c) 2006~2018 http://thinkphp.cn All rights reserved.\n"
	"// +----------------------------------------------------------------------\n"
	"// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )\n"
	"// +----------------------------------------------------------------------\n"
	"// | Author: liu21st <liu21st@gmail.com>\n"
	"// +----------------------------------------------------------------------\n"
	"\n"
	"namespace think\\model\\relation;\n"
	"\n"
	"use think\\db\\Query;\n"
	"use think\\Loader;\n"
	"use think\\Model;\n"
	"\n"
	"class HasOne extends OneToOne\n"
	"{\n"
	"    /**\n"
	"     * 构造函数\n"
	"     * @access public\n"
	"     * @param Model  $parent     上级模型对象\n"
	"     * @param string $model      模型名\n"
	"     * @param string $foreignKey 关联外键\n"
	"     * @param string $localKey   当前模型主键\n"
	"     * @param string $joinType   JOIN类型\n"
	"     */\n"
	"    public function __construct(Model $parent, $model, $foreignKey, $localKey, $joinType = \'INNER\')\n"
	"    {\n"
	"        $this->parent     = $parent;\n"
	"        $this->model      = $model;\n"
	"        $this->foreignKey = $foreignKey;\n"
	"        $this->localKey   = $localKey;\n"
	"        $this->joinType   = $joinType;\n"
	"        $this->query      = (new $model)->db();\n"
	"    }\n"
	"\n"
	"    /**\n"
	"     * 延迟获取关联数据\n"
	"     * @param string   $subRelation 子关联名\n"
	"     * @param \\Closure $closure     闭包查询条件\n"
	"     * @return array|false|\\PDOStatement|string|Model\n"
	"     */\n"
	"    public function getRelation($subRelation = \'\', $closure = null)\n"
	"    {\n"
	"        // 执行关联定义方法\n"
	"        $localKey = $this->localKey;\n"
	"        if ($closure) {\n"
	"            call_user_func_array($closure, [ & $this->query]);\n"
	"        }\n"
	"        // 判断关联类型执行查询\n"
	"        $relationModel = $this->query\n"
	"            ->removeWhereField($this->foreignKey)\n"
	"            ->where($this->foreignKey, $this->parent->$localKey)\n"
	"            ->relation($subRelation)\n"
	"            ->find();\n"
	"\n"
	"        if ($relationModel) {\n"
	"            $relationModel->setParent(clone $this->parent);\n"
	"        }\n"
	"\n"
	"        return $relationModel;\n"
	"    }\n"
	"\n"
	"    /**\n"
	"     * 根据关联条件查询当前模型\n"
	"     * @access public\n"
	"     * @return Query\n"
	"     */\n"
	"    public function has()\n"
	"    {\n"
	"        $table      = $this->query->getTable();\n"
	"        $model      = basename(str_replace(\'\\\\\', \'/\', get_class($this->parent)));\n"
	"        $relation   = basename(str_replace(\'\\\\\', \'/\', $this->model));\n"
	"        $localKey   = $this->localKey;\n"
	"        $foreignKey = $this->foreignKey;\n"
	"        return $this->parent->db()\n"
	"            ->alias($model)\n"
	"            ->whereExists(function ($query) use ($table, $model, $relation, $localKey, $foreignKey) {\n"
	"                $query->table([$table => $relation])->field($relation . \'.\' . $foreignKey)->whereExp($model . \'.\' . $localKey, \'=\' . $relation . \'.\' . $foreignKey);\n"
	"            });\n"
	"    }\n"
	"\n"
	"    /**\n"
	"     * 根据关联条件查询当前模型\n"
	"     * @access public\n"
	"     * @param  mixed  $where 查询条件（数组或者闭包）\n"
	"     * @param  mixed  $fields   字段\n"
	"     * @return Query\n"
	"     */\n"
	"    public function hasWhere($where = [], $fields = null)\n"
	"    {\n"
	"        $table    = $this->query->getTable();\n"
	"        $model    = basename(str_replace(\'\\\\\', \'/\', get_class($this->parent)));\n"
	"        $relation = basename(str_replace(\'\\\\\', \'/\', $this->model));\n"
	"\n"
	"        if (is_array($where)) {\n"
	"            foreach ($where as $key => $val) {\n"
	"                if (false === strpos($key, \'.\')) {\n"
	"                    $where[$relation . \'.\' . $key] = $val;\n"
	"                    unset($where[$key]);\n"
	"                }\n"
	"            }\n"
	"        }\n"
	"        $fields = $this->getRelationQueryFields($fields, $model);\n"
	"\n"
	"        return $this->parent->db()->alias($model)\n"
	"            ->field($fields)\n"
	"            ->join([$table => $relation], $model . \'.\' . $this->localKey . \'=\' . $relation . \'.\' . $this->foreignKey, $this->joinType)\n"
	"            ->where($where);\n"
	"    }\n"
	"\n"
	"    /**\n"
	"     * 预载入关联查询（数据集）\n"
	"     * @access public\n"
	"     * @param array    $resultSet   数据集\n"
	"     * @param string   $relation    当前关联名\n"
	"     * @param string   $subRelation 子关联名\n"
	"     * @param \\Closure $closure     闭包\n"
	"     * @return void\n"
	"     */\n"
	"    protected function eagerlySet(&$resultSet, $relation, $subRelation, $closure)\n"
	"    {\n"
	"        $localKey   = $this->localKey;\n"
	"        $foreignKey = $this->foreignKey;\n"
	"\n"
	"        $range = [];\n"
	"        foreach ($resultSet as $result) {\n"
	"            // 获取关联外键列表\n"
	"            if (isset($result->$localKey)) {\n"
	"                $range[] = $result->$localKey;\n"
	"            }\n"
	"        }\n"
	"\n"
	"        if (!empty($range)) {\n"
	"            $this->query->removeWhereField($foreignKey);\n"
	"            $data = $this->eagerlyWhere($this->query, [\n"
	"                $foreignKey => [\n"
	"                    \'in\',\n"
	"                    $range,\n"
	"                ],\n"
	"            ], $foreignKey, $relation, $subRelation, $closure);\n"
	"            // 关联属性名\n"
	"            $attr = Loader::parseName($relation);\n"
	"            // 关联数据封装\n"
	"            foreach ($resultSet as $result) {\n"
	"                // 关联模型\n"
	"                if (!isset($data[$result->$localKey])) {\n"
	"                    $relationModel = null;\n"
	"                } else {\n"
	"                    $relationModel = $data[$result->$localKey];\n"
	"                    $relationModel->setParent(clone $result);\n"
	"                    $relationModel->isUpdate(true);\n"
	"                }\n"
	"                if (!empty($this->bindAttr)) {\n"
	"                    // 绑定关联属性\n"
	"                    $this->bindAttr($relationModel, $result, $this->bindAttr);\n"
	"                } else {\n"
	"                    // 设置关联属性\n"
	"                    $result->setRelation($attr, $relationModel);\n"
	"                }\n"
	"            }\n"
	"        }\n"
	"    }\n"
	"\n"
	"    /**\n"
	"     * 预载入关联查询（数据）\n"
	"     * @access public\n"
	"     * @param Model    $result      数据对象\n"
	"     * @param string   $relation    当前关联名\n"
	"     * @param string   $subRelation 子关联名\n"
	"     * @param \\Closure $closure     闭包\n"
	"     * @return void\n"
	"     */\n"
	"    protected function eagerlyOne(&$result, $relation, $subRelation, $closure)\n"
	"    {\n"
	"        $localKey   = $this->localKey;\n"
	"        $foreignKey = $this->foreignKey;\n"
	"        $this->query->removeWhereField($foreignKey);\n"
	"        $data = $this->eagerlyWhere($this->query, [$foreignKey => $result->$localKey], $foreignKey, $relation, $subRelation, $closure);\n"
	"\n"
	"        // 关联模型\n"
	"        if (!isset($data[$result->$localKey])) {\n"
	"            $relationModel = null;\n"
	"        } else {\n"
	"            $relationModel = $data[$result->$localKey];\n"
	"            $relationModel->setParent(clone $result);\n"
	"            $relationModel->isUpdate(true);\n"
	"        }\n"
	"        if (!empty($this->bindAttr)) {\n"
	"            // 绑定关联属性\n"
	"            $this->bindAttr($relationModel, $result, $this->bindAttr);\n"
	"        } else {\n"
	"            $result->setRelation(Loader::parseName($relation), $relationModel);\n"
	"        }\n"
	"    }\n"
	"\n"
	"    /**\n"
	"     * 执行基础查询（仅执行一次）\n"
	"     * @access protected\n"
	"     * @return void\n"
	"     */\n"
	"    protected function baseQuery()\n"
	"    {\n"
	"        if (empty($this->baseQuery)) {\n"
	"            if (isset($this->parent->{$this->localKey})) {\n"
	"                // 关联查询带入关联条件\n"
	"                $this->query->where($this->foreignKey, \'=\', $this->parent->{$this->localKey});\n"
	"            }\n"
	"\n"
	"            $this->baseQuery = true;\n"
	"        }\n"
	"    }\n"
	"}\n"
	"\n"
;
#endif